package vacuum.lgadmin.jail;

import java.util.HashMap;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Server;
import org.bukkit.entity.Player;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerListener;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerRespawnEvent;
import org.bukkit.event.player.PlayerTeleportEvent;

public class JailPlayerListener extends PlayerListener
{

	private HashMap<String, Location> jailed = new HashMap<String, Location>();

	private Location jailLocation;

	public JailPlayerListener(HashMap<String, Location> jailed, Location jailLocation)
	{
		this.jailed = jailed;
		this.jailLocation = jailLocation;
	}

	/**
	 * Jails a player.
	 */
	public boolean jailPlayer(Player player, String reason){
		if(jailed.containsKey(player.getName().toLowerCase()))
			return false;
		Location loc = player.getLocation();
		player.teleport(jailLocation);
		jailed.put(player.getName().toLowerCase(), loc);
		player.sendMessage(ChatColor.RED + "You've been locked up" + ((reason == null) ? "" : " for " + reason) + ".");
		Bukkit.getServer().broadcast(Server.BROADCAST_CHANNEL_ADMINISTRATIVE, player.getName() + " has been jailed.");
		return true;
	}

	/**
	 * Frees a player.
	 */
	public void freePlayer(Player player){
		if(jailed.containsKey(player.getName().toLowerCase())){
			player.teleport(jailed.remove(player.getName().toLowerCase()));
			player.sendMessage(ChatColor.GREEN + "You're free!");
			Bukkit.getServer().broadcast(Server.BROADCAST_CHANNEL_ADMINISTRATIVE, player.getName() + " has been freed.");
		}
	}

	@Override
	public void onPlayerInteract(PlayerInteractEvent event)
	{

		if((event.getAction().equals(Action.LEFT_CLICK_BLOCK) || event.getAction().equals(Action.RIGHT_CLICK_BLOCK)) && jailed.containsKey(event.getPlayer().getName().toLowerCase())){
			event.setCancelled(true);
		}
	}

	@Override
	public void onPlayerMove(PlayerMoveEvent event)
	{
		//TODO:
//		Location to = event.getTo();
//		Location from = event.getFrom();
//		if((from.getBlockX() != to.getBlockX() || from.getBlockY() != to.getBlockY() || from.getBlockZ() != to.getBlockZ()) && jailed.containsKey(event.getPlayer().getName().toLowerCase())){
//			System.out.println("Blocking " + event.getType().toString().toLowerCase() + " event");
//			event.setCancelled(true);
//		}
	}

	@Override
	public void onPlayerRespawn(final PlayerRespawnEvent event)
	{
		if(jailed.containsKey(event.getPlayer().getName().toLowerCase())){
			//get around all the plugins that give themselves unoverridable authority (MyHome *cough cough*)
			new Thread(new Runnable()
			{
				
				@Override
				public void run()
				{
					try
					{
						Thread.sleep(1000);
					} catch (InterruptedException e)
					{
						e.printStackTrace();
					}
					event.getPlayer().teleport(jailLocation);
				}
			}).start();
		}
	}

	@Override
	public void onPlayerTeleport(PlayerTeleportEvent event)
	{
		if(jailed.containsKey(event.getPlayer().getName().toLowerCase())){
			event.setCancelled(true);
		}
	}

	@Override
	public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event)
	{
		if(jailed.containsKey(event.getPlayer().getName().toLowerCase())){
			event.setCancelled(true);
			event.getPlayer().sendMessage("You may not use commands while in jail!");
		}
	}

	public HashMap<String, Location> getJailedPlayers(){
		return jailed;
	}



}
